<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php


class MServerToken extends MModel
{
    
    
    public static function initServerToken($db_data) {
        if ($db_data === null) {
            return null;
        }
        $token_info = new MUser();
        $token_info->ost_id              = $db_data["ost_id"];
        $token_info->ost_osr_id_ref      = $db_data["ost_osr_id_ref"];
        $token_info->ost_usa_id_ref      = $db_data["ost_usa_id_ref"];
        $token_info->ost_token           = $db_data["ost_token"];
        $token_info->ost_token_secret    = $db_data["ost_token_secret"];
        $token_info->ost_token_type      = $db_data["ost_token_type"];
        $token_info->ost_verifier        = $db_data["ost_verifier"];
        return $token_info;
    }
    
    
    
    public function queryOauthByToken($token) {
        $db = MDbManager::getInstance();
        $sql = "select * from ".DB_PREFIX."_server_token where ost_token =\"{$token}\"";
        $db_data = $db->selectDb($sql);
        
        FXP::trace("queryOauthByToken:".$sql);
        if (empty($db_data)){
            return false;
        }

        return self::assembleServerToken($db_data[0]);
    }
    
    
    
    public function queryOauthByTokenAndTokenType($token, $ost_token_type) {
        $db = MDbManager::getInstance();
        $sql = "select * from ".DB_PREFIX."_server_token where ost_token =\"{$token}\" and ost_token_type = \"{$ost_token_type}\"";
        $db_data = $db->selectDb($sql);
        FXP::trace("queryOauthByTokenAndTokenType:".$sql);
        if (empty($db_data)){
            return false;
        }

        return self::assembleServerToken($db_data[0]);
    }

    
    
    public function deleteOauthByTokenAndTokenType($ost_usa_id, $ost_token_type) {
        $db = MDbManager::getInstance();
        $sql = "delete from ".DB_PREFIX."_server_token where ost_usa_id_ref =$ost_usa_id and ost_token_type = \"{$ost_token_type}\"";
        FXP::trace("deleteOauthByTokenAndTokenType:".$sql);
        $db->deleteDb($sql);
    }

    
    public function queryOauthByIdAndTokenType($ost_usa_id, $ost_token_type) {
        $db = MDbManager::getInstance();
        $sql = "select * from ".DB_PREFIX."_server_token where ost_usa_id_ref =$ost_usa_id and ost_token_type = \"{$ost_token_type}\"";
        FXP::trace("queryOauthByIdAndTokenType:".$sql);
        $db_data = $db->selectDb($sql);

        return $db_data;
    }
    
    
    private function assembleServerToken($db_data){
        $this->ost_id              = $db_data["ost_id"];
        $this->ost_osr_id_ref      = $db_data["ost_osr_id_ref"];
        $this->ost_usa_id_ref      = $db_data["ost_usa_id_ref"];
        $this->ost_token           = $db_data["ost_token"];
        $this->ost_token_secret    = $db_data["ost_token_secret"];
        $this->ost_token_type      = $db_data["ost_token_type"];
        $this->ost_verifier        = $db_data["ost_verifier"];

        return $this;
    }
}
?>